#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    char buffer[4];
    int fd1, fd2;
    int ret;

    fd1 = open(argv[1], O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    if(-1 == fd1){
        perror("open error");
        exit(-1);
    }

    fd2 = open(argv[1], O_RDWR);
    if(-1 == fd2){
        perror("open error");
        ret = -1;
        goto err1;
    }

    buffer[0] = 0x11;
    buffer[1] = 0x22;
    buffer[2] = 0x33;
    buffer[3] = 0x44;

    ret = write(fd1, buffer, 4);
    if(-1 == ret){
        perror("write error");
        goto err2;
    }

    // ret = lseek(fd2, 0, SEEK_SET);
    // if(-1 == ret){
    //     perror("lseek error");
    //     goto err2;
    // }

    memset(buffer, 0x00, sizeof(buffer));
    ret = read(fd2, buffer, 4);
    if(-1 == ret){
        perror("read error");
        goto err2;
    }

    printf("0x%x, 0x%x, 0x%x, 0x%x\n", buffer[0],buffer[1],buffer[2],buffer[3]);

    ret = 0;

    err2:
        close(fd2);
        exit(ret);

    err1:
        close(fd1);
        exit(ret);

    close(fd1);
    close(fd2);

    exit(0);
}
